/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spreadsheet.gui;

import com.eteks.jeks.JeksExpressionSyntax;
import com.eteks.jeks.JeksFunctionSyntax;
import com.eteks.jeks.JeksTable;
import com.eteks.parser.CompilationException;
import com.eteks.parser.CompiledFunction;
import com.eteks.tools.swing.HTMLDocumentViewer;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import spreadsheet.spreadsheet.Table.Spreadsheet;

/**
 *
 * @author Nobody
 */
public class JeksJMenuBar extends JMenuBar {

    private JMenu toolsMenu = new JMenu();
    private JMenu helpMenu = new JMenu("Help");
    private JMenu columnMenu = new JMenu("Column");
    private JMenu rowMenu = new JMenu("Row");
    private JMenuItem addColumn = new JMenuItem("Add Column");
    private JMenuItem addRow = new JMenuItem("Add Row");
    private JMenuItem newFunctionMenuItem = new JMenuItem();
    private JMenuItem helpMenuItem = new JMenuItem();
    private ResourceBundle resourceBundle = ResourceBundle.getBundle("com.eteks.jeks.resources.jeks");
    private HTMLDocumentViewer helpViewer;
    private JTable table;

    public JeksJMenuBar(final JTable table) {
        this.table = table;

        loadMenuLabel(newFunctionMenuItem, "TOOLS_NEW_FUNCTION_MENU");
        loadMenuLabel(helpMenuItem, "HELP_HELP_MENU");
        loadMenuLabel(toolsMenu, "TOOLS_MENU");

        newFunctionMenuItem.addActionListener(newFunctionAction);
        helpMenuItem.addActionListener(helpAction);
        addRow.addActionListener(addRowAction);
        addColumn.addActionListener(addColumnAction);

        toolsMenu.add(newFunctionMenuItem);
        helpMenu.add(helpMenuItem);
        columnMenu.add(addColumn);
        rowMenu.add(addRow);

        add(columnMenu);
        add(rowMenu);
        add(toolsMenu);
        add(helpMenu);

    }

    private void loadMenuLabel(JMenuItem menuItem, String resource) {
        String menuString = resourceBundle.getString(resource);
        int mnemonicIndex = menuString.indexOf('&');
        if (mnemonicIndex >= 0) {
            menuItem.setText(menuString.substring(0, mnemonicIndex) + menuString.substring(mnemonicIndex + 1));
            menuItem.setMnemonic(menuString.charAt(mnemonicIndex + 1));
        } else {
            menuItem.setText(menuString);
        }
    }
    private ActionListener addRowAction = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ((Spreadsheet) table).addRow();
        }
    };
    private ActionListener addColumnAction = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ((Spreadsheet) table).addColumn();
        }
    };
    private ActionListener helpAction = new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            if (helpViewer == null) {
                helpViewer = new HTMLDocumentViewer();
                helpViewer.setPage(ClassLoader.getSystemResource(resourceBundle.getString("HELP_FILE")));
                Rectangle jeksBounds = getBounds();
                jeksBounds.x += 20;
                jeksBounds.y += 20;
                jeksBounds.height = 400;
                helpViewer.setBounds(jeksBounds);

            }
            helpViewer.setVisible(true);
        }
    };
    private ActionListener newFunctionAction = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent event) {
            createNewFunction();
        }
    };

    private void createNewFunction() {
        String functionDef = "";
        CompiledFunction function = null;
        if (table instanceof JeksTable) {
            while (function == null) {
                try {
                    if ((functionDef = (String) JOptionPane.showInputDialog(this.getParent(), resourceBundle.getString("NEW_FUNCTION_DIALOG_TEXT"), resourceBundle.getString("NEW_FUNCTION_DIALOG_TITLE"), JOptionPane.PLAIN_MESSAGE,
                            null, null, functionDef)) == null) {
                        return;
                    }
                    function = ((JeksTable) table).getExpressionParser().getFunctionParser().compileFunction(functionDef);
                    ((JeksTable) table).getExpressionParser().addUserFunction(function);
                } catch (CompilationException ex) {
                    JeksFunctionSyntax syntax = (JeksFunctionSyntax) ((JeksTable) table).getExpressionParser().getFunctionParser().getSyntax();
                    String errorMessage = syntax.getExceptionMessage(ex);
                    JOptionPane.showMessageDialog(this.getParent(), errorMessage, syntax.getMessage(JeksExpressionSyntax.MESSAGE_COMPILATION_ERROR_TITLE), JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }
}
